// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.randomizedWorld.randomizedBuilding.TableStories;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import zombie.core.Rand;
import zombie.iso.IsoDirections;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.randomizedWorld.randomizedBuilding.RandomizedBuildingBase;

public class RBTableStoryBase extends RandomizedBuildingBase {
    public static ArrayList<RBTableStoryBase> allStories = new ArrayList();
    public static int totalChance = 0;
    protected int chance = 0;
    protected ArrayList<String> rooms = new ArrayList();
    protected boolean need2Tables = false;
    protected boolean ignoreAgainstWall = false;
    protected IsoObject table2 = null;
    protected IsoObject table1 = null;
    protected boolean westTable = false;
    private static final HashMap<RBTableStoryBase, Integer> rbtsmap = new HashMap();
    private static final ArrayList<IsoObject> tableObjects = new ArrayList();
    public ArrayList<HashMap<String, Integer>> fullTableMap = new ArrayList();

    public static void initStories(IsoGridSquare sq, IsoObject table) {
        if (allStories.isEmpty()) {
            allStories.add(new RBTSBreakfast());
            allStories.add(new RBTSDinner());
            allStories.add(new RBTSSoup());
            allStories.add(new RBTSSewing());
            allStories.add(new RBTSElectronics());
            allStories.add(new RBTSFoodPreparation());
            allStories.add(new RBTSButcher());
            allStories.add(new RBTSSandwich());
            allStories.add(new RBTSDrink());
        }

        totalChance = 0;
        rbtsmap.clear();

        for (int _int = 0; _int < allStories.size(); _int++) {
            RBTableStoryBase rBTableStoryBase = (RBTableStoryBase)allStories.get(_int);
            if (rBTableStoryBase.isValid(sq, table, false) && rBTableStoryBase.isTimeValid(false)) {
                totalChance = totalChance + rBTableStoryBase.chance;
                rbtsmap.put(rBTableStoryBase, rBTableStoryBase.chance);
            }
        }
    }

    public static RBTableStoryBase getRandomStory(IsoGridSquare sq, IsoObject table) {
        initStories(sq, table);
        int int0 = Rand.Next(totalChance);
        Iterator iterator = rbtsmap.keySet().iterator();
        int int1 = 0;

        while (iterator.hasNext()) {
            RBTableStoryBase rBTableStoryBase = (RBTableStoryBase)iterator.next();
            int1 += rbtsmap.get(rBTableStoryBase);
            if (int0 < int1) {
                rBTableStoryBase.table1 = table;
                return rBTableStoryBase;
            }
        }

        return null;
    }

    public boolean isValid(IsoGridSquare sq, IsoObject table, boolean force) {
        if (force) {
            return true;
        } else if (this.rooms != null && sq.getRoom() != null && !this.rooms.contains(sq.getRoom().getName())) {
            return false;
        } else {
            if (this.need2Tables) {
                this.table2 = this.getSecondTable(table);
                if (this.table2 == null) {
                    return false;
                }
            }

            return !this.ignoreAgainstWall || !sq.getWallFull();
        }
    }

    public IsoObject getSecondTable(IsoObject _table1) {
        this.westTable = true;
        IsoGridSquare isoGridSquare0 = _table1.getSquare();
        if (this.ignoreAgainstWall && isoGridSquare0.getWallFull()) {
            return null;
        } else {
            _table1.getSpriteGridObjects(tableObjects);
            IsoGridSquare isoGridSquare1 = isoGridSquare0.getAdjacentSquare(IsoDirections.W);
            IsoObject isoObject = this.checkForTable(isoGridSquare1, _table1, tableObjects);
            if (isoObject == null) {
                isoGridSquare1 = isoGridSquare0.getAdjacentSquare(IsoDirections.E);
                isoObject = this.checkForTable(isoGridSquare1, _table1, tableObjects);
            }

            if (isoObject == null) {
                this.westTable = false;
            }

            if (isoObject == null) {
                isoGridSquare1 = isoGridSquare0.getAdjacentSquare(IsoDirections.N);
                isoObject = this.checkForTable(isoGridSquare1, _table1, tableObjects);
            }

            if (isoObject == null) {
                isoGridSquare1 = isoGridSquare0.getAdjacentSquare(IsoDirections.S);
                isoObject = this.checkForTable(isoGridSquare1, _table1, tableObjects);
            }

            return isoObject != null && this.ignoreAgainstWall && isoGridSquare1.getWallFull() ? null : isoObject;
        }
    }

    private IsoObject checkForTable(IsoGridSquare isoGridSquare, IsoObject isoObject0, ArrayList<IsoObject> arrayList) {
        if (isoGridSquare == null) {
            return null;
        } else if (isoGridSquare.isSomethingTo(isoObject0.getSquare())) {
            return null;
        } else {
            for (int _int = 0; _int < isoGridSquare.getObjects().size(); _int++) {
                IsoObject isoObject1 = isoGridSquare.getObjects().get(_int);
                if ((arrayList.isEmpty() || arrayList.contains(isoObject1))
                    && isoObject1.getProperties().isTable()
                    && isoObject1.getContainer() == null
                    && isoObject1 != isoObject0) {
                    return isoObject1;
                }
            }

            return null;
        }
    }
}
